package cn.jsj.cxsys.controller;

import cn.jsj.cxsys.pojo.*;
import org.apache.poi.xssf.streaming.*;
import org.apache.poi.ss.usermodel.*;
import java.util.*;

public class ExcelWriter
{
    private static List<String> CELL_HEADS;

    public static Workbook exportData(final List<Record> dataList) {
        final Workbook workbook = (Workbook)new SXSSFWorkbook();
        final Sheet sheet = buildDataSheet(workbook);
        int rowNum = 1;
        for (final Record data : dataList) {
            if (data == null) {
                continue;
            }
            final Row row = sheet.createRow(rowNum++);
            convertDataToRow(data, row);
        }
        return workbook;
    }

    private static Sheet buildDataSheet(final Workbook workbook) {
        final Sheet sheet = workbook.createSheet();
        for (int i = 0; i < ExcelWriter.CELL_HEADS.size(); ++i) {
            sheet.setColumnWidth(i, 4000);
        }
        sheet.setDefaultRowHeight((short)400);
        final CellStyle cellStyle = buildHeadCellStyle(sheet.getWorkbook());
        final Row head = sheet.createRow(0);
        for (int j = 0; j < ExcelWriter.CELL_HEADS.size(); ++j) {
            final Cell cell = head.createCell(j);
            cell.setCellValue((String)ExcelWriter.CELL_HEADS.get(j));
            cell.setCellStyle(cellStyle);
        }
        return sheet;
    }

    private static CellStyle buildHeadCellStyle(final Workbook workbook) {
        final CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(BorderStyle.THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderRight(BorderStyle.THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(BorderStyle.THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        final Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        return style;
    }

    private static void convertDataToRow(final Record data, final Row row) {
        int cellNum = 0;
        Cell cell = row.createCell(cellNum++);
        if (data.getUser().getName() != null) {
            cell.setCellValue(data.getUser().getName());
        }
        cell = row.createCell(cellNum++);
        if (data.getUser().getClassName() != null) {
            cell.setCellValue(data.getUser().getClassName());
        }
        cell = row.createCell(cellNum++);
        if (data.getLab() != null && data.getLab().getLabNum() != null) {
            cell.setCellValue(data.getLab().getLabNum());
        }
        cell = row.createCell(cellNum++);
        if (data.getLab() != null) {
            if (data.getComputerNum() == -1) {
                cell.setCellValue("\u672a\u4f7f\u7528\u7535\u8111");
            }
            else {
                cell.setCellValue((double)data.getComputerNum());
            }
        }
        cell = row.createCell(cellNum++);
        if (data.getLab() != null && data.getInTime() != null) {
            cell.setCellValue(data.getInTime());
        }
        cell = row.createCell(cellNum++);
        if (data.getLab() != null && data.getOutTime() != null && !data.getOutTime().equals("")) {
            cell.setCellValue(data.getOutTime());
        }
        else {
            cell.setCellValue("\u5c1a\u672a\u79bb\u5f00");
        }
        cell = row.createCell(cellNum++);
        if (data.getLab() != null && data.getActive() != null) {
            cell.setCellValue(data.getActive());
        }
    }

    static{
        // 类装载时就载入指定好的列头信息，如有需要，可以考虑做成动态生成的列头
        CELL_HEADS = new ArrayList<>();
        CELL_HEADS.add("姓名");
        CELL_HEADS.add("班级");
        CELL_HEADS.add("实验室门牌号");
        CELL_HEADS.add("电脑编号");
        CELL_HEADS.add("签到时间");
        CELL_HEADS.add("签退时间");
        CELL_HEADS.add("活动信息");
    }
}
